package jsyf.oa.web.business;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.FastJsonConvert;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.business.BusinessFacade;

@Api(value = "商务条件申请接口", description = "商务条件申请接口")
@Controller
@RequestMapping("/business")
public class BusinessController {
    @Resource
    private BusinessFacade businessFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.businessFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = businessFacade.delById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/getBusinessList.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取商务预申请列表",
            notes = "获取商务预申请列表",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getBusinessList(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> result = this.businessFacade.getBusinessList();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/getBusinessListByPage.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "分页获取商务预申请列表",
            notes = "分页获取商务预申请列表",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getBusinessListByPage(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.businessFacade.getBusinessListByPage(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/getBusinessById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "根据ID获取商务预申请详情",
            notes = "根据ID获取商务预申请详情",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getBusinessById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.businessFacade.getBusinessById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }
    @ApiOperation(
            value = "获取商务条件未读数据",
            notes = "获取商务条件未读数据",
            httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "2017121618514C22B68296F31AF8DDE2", dataType = "String", paramType = "query")
    @RequestMapping(value = "/getUnDoCount.json", method = RequestMethod.GET)
    public void getUnDoCount(HttpServletResponse response, @RequestParam String userId) {
        String count = businessFacade.getUnDoCount(userId);
        JSONObject records = new JSONObject();
        records.put("count", count);
        if (count != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }


    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    public void getById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String id = obj.getString("id");
        if (StringUtils.isBlank(id)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
            return;
        }
        JSONObject records = this.businessFacade.getById(id);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/getJSONByAuditAndCopier.json", method = RequestMethod.POST)
    public void getJSONByAuditAndCopier(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String id = obj.getString("userId");
        if (StringUtils.isBlank(id)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
            return;
        }
        JSONObject records = this.businessFacade.getJSONByAuditAndCopier(id);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/applyAudit.json", method = RequestMethod.POST)
    public void applyAudit(HttpServletResponse response, @RequestBody String jsonData) {
        boolean result = businessFacade.applyAudit(jsonData);
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("审核失败"));
        }
    }




    /*
    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String id = businessFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/getUnDoCount.json", method = RequestMethod.GET)
    public void getUnDoCount(HttpServletResponse response, @RequestParam String userId) {
        String count = businessFacade.getUnDoCount(userId);
        JSONObject records = new JSONObject();
        records.put("count", count);
        if (count != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }

    @RequestMapping(value = "/getByPage.json", method = RequestMethod.POST)
    public void getByPage(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        String requestPage = obj.getString("requestPage");
        String pageSize = obj.getString("pageSize");
        String key = obj.getString("key");
        String companyId = obj.getString("companyId");
        String auditFlag = obj.getString("auditFlag");
        String type = obj.getString("type");
        String createUserName = obj.getString("createUserName");
        if (StringUtils.isEmpty(userId)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("用户Id为空"));
            return;
        }
        if (StringUtils.isEmpty(requestPage)) {
            requestPage = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = Integer.MAX_VALUE + "";
        }
        JSONObject records = this.businessFacade.getByPage(userId, key, companyId, auditFlag, type, createUserName, Integer.parseInt(requestPage), Integer.parseInt(pageSize));
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }



    @RequestMapping(value = "/applyAudit.json", method = RequestMethod.POST)
    public void applyAudit(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        boolean result = businessFacade.applyAudit(jsonObject.getString("id"), jsonObject.getString("userId"), jsonObject.getString("auditFlag"), jsonObject.getString("advise"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("审核失败"));
        }
    }

    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    public void removeById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Boolean result = businessFacade.removeById(jsonObject.getString("id"));
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/copierReturn.json", method = RequestMethod.POST)
    public void copierReturn(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Boolean result = businessFacade.copierReturn(jsonObject.getString("id"), jsonObject.getString("copierReturnReason"));
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/getBrandByCompanyId.json", method = RequestMethod.GET)
    public void getBrandByCompanyId(HttpServletResponse response, @RequestParam String companyId) {
        List<JSONObject> records = businessFacade.getBrandByCompanyId(companyId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
    }

    @RequestMapping(value = "/getModelByBrandId.json", method = RequestMethod.GET)
    public void getModelByBrandId(HttpServletResponse response, @RequestParam String brandId) {
        List<JSONObject> records = businessFacade.getModelByBrandId(brandId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
    }
    */
}
